package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.controller.form.DefecttypeForm;
import com.woniuxy.controller.info.SeverityInfo;
import com.woniuxy.controller.info.TypeInfo;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.dao.DefectDao;
import com.woniuxy.dao.UserDao;
import com.woniuxy.model.Defectseverity;
import com.woniuxy.model.Defectstate;
import com.woniuxy.model.Defecttype;
import com.woniuxy.dao.DefecttypeDao;
import com.woniuxy.model.User;
import com.woniuxy.service.DefecttypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class DefecttypeServiceImpl extends ServiceImpl<DefecttypeDao, Defecttype> implements DefecttypeService {
    @Resource
    private DefecttypeDao defecttypeDao;
    @Resource
    private UserDao userDao;
    @Override
    public boolean change(DefecttypeForm defecttypeForm) {
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account_id", userInfo.getId()));
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        Defecttype defecttype = new Defecttype();
        defecttype.setDefecttypeId(defecttypeForm.getDefecttypeId());
        defecttype.setDefecttypeName(defecttypeForm.getDefecttypeName());
        defecttype.setDefecttypeNum(defecttypeForm.getDefecttypeNum());
        defecttype.setDefecttypeChangenameId(user.getUId());
        defecttype.setDefecttypeChangetime(nowTimeToString);
        defecttype.setDefecttypeChangename(user.getUName());

        Integer i=defecttypeDao.updateById(defecttype);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<TypeInfo> findAll() {
        ArrayList<TypeInfo> typeInfos = new ArrayList<>();
        List<Defecttype> defecttypes = defecttypeDao.selectAll();
        for (Defecttype defecttype:defecttypes){
            TypeInfo typeInfo = CopyUtil.copyOne(defecttype, TypeInfo.class);
            if (defecttype.getDefecttypeState()==1){
                typeInfo.setDefecttypeNewState("有效");
            }else{
                typeInfo.setDefecttypeNewState("禁用");
            }
            typeInfos.add(typeInfo);
        }

        return typeInfos;
    }

    @Override
    public Integer add(DefecttypeForm defecttypeForm) {
        defecttypeForm.setDefecttypeState(1);
        Defecttype defecttype = CopyUtil.copyOne(defecttypeForm, Defecttype.class);
        int insert = defecttypeDao.insert(defecttype);
        return insert;
    }

    @Override
    public Integer delectByIds(List<Integer> ids) {
        int i = this.baseMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public Integer changeState(DefecttypeForm defecttypeForm) {
        String state = defecttypeForm.getDefecttypeNewState();

        if(state.equals("有效")){
            defecttypeForm.setDefecttypeState(0);
        }else if(state.equals("禁用")){
            defecttypeForm.setDefecttypeState(1);
        }
        Defecttype defecttype = CopyUtil.copyOne(defecttypeForm, Defecttype.class);
        UpdateWrapper<Defecttype> wrapper = new UpdateWrapper<>();
        wrapper.eq("defectType_name",defecttype.getDefecttypeName()).set("defectType_state",defecttype.getDefecttypeState());
        int update = defecttypeDao.update(null, wrapper);
        return update;

    }
}
